﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcreport.Web
{
    public partial class tj : System.Web.UI.Page
    {
        lcreport.BLL.reportinfo bll = new lcreport.BLL.reportinfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["admin"] == null)
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请先登录！", "admin/login.aspx");

            }
            else
            {
                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }
            

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            bll.DeleteList(idlist);
            BindData();
        }

       

        public void BindData()
        {
            DataSet ds = getData();
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        private DataSet getData()
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and rtitle like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtrwork.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and rwork like '%{0}%'", txtrwork.Text.Trim());
            }
            if (txtstart.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and rdate >='{0}'", txtstart.Text);

            }
            if (txtend.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and rdate <='{0}'", txtend.Text);

            }
            if (reportQuestionId1.Text.Trim() != "0")
            {
                strWhere.AppendFormat(" and rtype ='{0}'", reportQuestionId1.Text);

            }
            //robject
            if (DropDownList1.SelectedValue != "0")
            {
                strWhere.AppendFormat(" and robject ='{0}'", DropDownList1.Text);

            }
            strWhere.Append(" order by rdate desc");
            ds = bll.GetList(strWhere.ToString());
            return ds;
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl = (Label)e.Row.FindControl("Label1");
                DataRowView drv = (DataRowView)e.Row.DataItem;

                string uid = drv["uid"].ToString();

                if (uid == "" || uid == "0") lbl.Text = "匿名举报";
                else {
                    lbl.Text = "实名名举报";

                }
               // Label.Text=               

            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }
        protected void btnOut_Click(object sender, EventArgs e)
        {
            //gridview1.BottomPagerRow.Visible = false;//导出到Excel表后，隐藏分页部分

            gridView.Columns[0].Visible = false;//隐藏“编辑”列
            gridView.Columns[9].Visible = false;
            gridView.Columns[10].Visible = false;//隐藏“删除”列

            gridView.AllowPaging = false;//取消分页，便于导出所有数据，不然只能导出当前页面的几条数据
            DataSet ds= getData();
            gridView.DataSource = ds;//取消分页后重新绑定数据集,ds为数据集dataset
            gridView.DataBind();


            string f = DateTime.Now.Ticks.ToString();
            ExportExcel(gridView, f);
            gridView.DataSource = ds;
            gridView.AllowPaging = true;
            gridView.Columns[0].Visible = true;//隐藏“编辑”列
            gridView.Columns[9].Visible = true;
            gridView.Columns[10].Visible = true;//隐藏“删除”
            gridView.DataBind();

        }
        public void ExportExcel(System.Web.UI.WebControls.WebControl objControl, string strFileName)
        {

            strFileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.Charset = "gb2312";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "online; filename=" + strFileName + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";

            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            objControl.RenderControl(oHtmlTextWriter);

            objControl = null;
            System.Web.HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=gb2312\"/>" + oStringWriter.ToString().Replace("<td", "<td STYLE='MSO-NUMBER-FORMAT:\\@'"));
            System.Web.HttpContext.Current.Response.Buffer = false;
            System.Web.HttpContext.Current.Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //override VerifyRenderingInServerForm.
        }
        protected void btncl_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            string r=txtjg.Text.Trim();
            bll.ResultList(idlist,r);
            BindData();

        }
    }
}